#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from SinaWeiboAPI.sinaAPI import SinaAPI
from SinaWeiboAPI import oauth
import unittest
import simplejson as json


class TimelineTest(unittest.TestCase):
    """
    Test for the statuses operation
    Statuses/public_timeline
    Statuses/friends_timeline
    Statuses/user_timeline
    Statuses/mentions
    Statuses/comments_timeline
    Statuses/comments_by_me
    Statuses/comments_to_me
    Statuses/comments
    Statuses/counts
    Statuses/repost_timeline
    Statuses/repost_by_me
    Statuses/unread
    Statuses/reset_count
    Emotions
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 10)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)

    def testStatusesPublicTimeline(self):
        """test for statuses/public_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.public_timeline_statuses(count=5, base_app=0)
        self.assertTrue(res[0].id is not None)
        for weibo in res:
            print weibo.text
  
    def testStatusesFriendsTimeline(self):
        """test for statuses/friends_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.friends_timeline_statuses(count=5, base_app=0, \
                                               feature=0)
        self.assertTrue(res[0].id is not None)
        for weibo in res:
            print weibo.text
    
    def testStatusesUserTimeline(self):
        """test for statuses/user_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.user_timeline_statuses(count=5, base_app=0, \
                                             feature=0)
        self.assertTrue(res[0].id is not None)
        for weibo in res:
            print weibo.text
            print weibo.id
        res = testAPI.user_timeline_statuses(count=5, base_app=0, \
                                           feature=0, \
                                           user_id=2123116482)
        self.assertTrue(res[0].id is not None)
        for weibo in res:
            print weibo.text
            print weibo.id
        res = testAPI.user_timeline_statuses(count=5, base_app=0, \
                                           feature=0, \
                                           screen_name='刀尔锋')
        self.assertTrue(res[0].id is not None)
        for weibo in res:
            print weibo.text
            print weibo.id

    def testStatusesMentions(self):
        """test for statuses/mentions"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.mentions_statuses(count=5) 
        self.assertTrue(res is not None)

    def testStatusesCommentsTimeline(self):
        """test for statuses/comments_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.comments_timeline_statuses(count=5)
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text

    def testStatusesCommentsByMe(self):
        """test for statuses/comments_by_me"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.comments_by_me_statuses(count=5)
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text
            print weibo.id
            print weibo.status.id

    def testStatusesCommentsToMe(self):
        """test for statuses/comments_to_me"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.comments_to_me_statuses(count=5)
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text
            print weibo.id
            print weibo.status.id

    def testStatusesComments(self):
        """test for statuses/comments"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.comments_statuses(id=10736464264, count=5)
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text
    
    def testStatusesCounts(self):
        """test for statuses/counts"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.counts_statuses(ids='10807110245,10736464264')
        self.assertTrue(res is not None)
        for count in res:
            print count.id
            print count.comments

    def testStatusesRepostTimeline(self):
        """test for statuses/repost_timeline"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.repost_timeline_statuses(id='10736464264')
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text 
    
    def testStatusesRepostByMe(self):
        """test for statuses/repost_by_me"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.repost_by_me_statuses(id='2099655804')
        self.assertTrue(res is not None)
        for weibo in res:
            print weibo.text 

    def testStatusesUnread(self):
        """test for statuses/unread"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.unread_statuses(with_new_status=1)
        self.assertTrue(res is not None)
        print res.new_status

    def testStatusesResetCount(self):
        """test for statuses/reset_count"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.reset_count_statuses(type=1)
        self.assertTrue(res is not None)
        print res.result
    
    def testEmotions(self):
        """test for emotions"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = SinaAPI(consumer, access_token)
        res = testAPI.emotions(type='face')
        self.assertTrue(res is not None)
        for emotion in res:
            print emotion.phrase

if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(TimelineTest('testStatusesCommentsToMe'))
    unittest.TextTestRunner().run(fast)
